using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Customers
{
    [Authorize]
    public class CustomerTypeAppService : BasicArchivesAppService, ICustomerTypeAppService
    {
        protected ICustomerTypeRepository CustomerTypeRepository => LazyServiceProvider.LazyGetRequiredService<ICustomerTypeRepository>();
        protected CustomerTypeManager CustomerTypeManager => LazyServiceProvider.LazyGetRequiredService<CustomerTypeManager>();

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public async Task<CustomerTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<CustomerType, CustomerTypeDto>(
                await CustomerTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public async Task<CustomerTypeDto> FindAsync(string filter)
        {
            var entity = await CustomerTypeRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<CustomerType, CustomerTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public async Task<CustomerTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await CustomerTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<CustomerType, CustomerTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public async Task<PagedResultDto<CustomerTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await CustomerTypeRepository.GetCountAsync(true);
            var list = await CustomerTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<CustomerTypeDto>(
                totalCount,
                ObjectMapper.Map<List<CustomerType>, List<CustomerTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public async Task<PagedResultDto<CustomerTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<CustomerType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<CustomerType>(input.FilterGroup);
            }

            var totalCount = await CustomerTypeRepository.GetCountAsync(expression, true);
            var list = await CustomerTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<CustomerTypeDto>(
                totalCount,
                ObjectMapper.Map<List<CustomerType>, List<CustomerTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Query)]
        public virtual async Task<ListResultDto<CustomerTypeDto>> GetChildrenAsync(Guid? parentId, bool recursive = false)
        {
            var list = ObjectMapper.Map<List<CustomerType>, List<CustomerTypeDto>>(
                await CustomerTypeRepository.GetChildrenAsync(parentId, false, recursive)
            ).OrderBy(x => x.Code).ToList();

            return new ListResultDto<CustomerTypeDto>(list);
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Create)]
        public async Task<CustomerTypeDto> CreateAsync(CustomerTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<CustomerTypeCreateDto, CustomerType>(input);
            input.MapExtraPropertiesTo(entity);

            await CustomerTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Update)]
        public async Task<CustomerTypeDto> UpdateAsync(Guid id, CustomerTypeUpdateDto input)
        {
            var entity = await CustomerTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            input.MapExtraPropertiesTo(entity);

            await CustomerTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.CustomerType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await CustomerTypeManager.DeleteAsync(id);
        }
    }
}